/*
 * Fichier: Disque.java
 * Crée le: 5 avril 2012.
 * Modifié: 5 avril 2012.
 * Auteurs: Sébastien ESTIENNE.
 * SiteWeb: http://www.prog-info.org/
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package chapitre13;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.text.NumberFormat;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * 
 * @author Sébastien ESTIENNE.
 */
public class Disque extends JFrame {

    /** <p>Serial version UID.</p> */
    private static final long serialVersionUID = 1L;
    
    private JLabel rayon;
    private JLabel perimetre;
    private JLabel surface;
    private JLabel valeurRayon;
    private JLabel valeurPerimetre;
    private JLabel valeurSurface;
    private JSlider slider;
    private DisquePanel disque;
    private NumberFormat format;
    
    public Disque() {
        super("Disque");
        
        format = NumberFormat.getInstance();
        format.setMinimumFractionDigits(1);
        format.setMaximumFractionDigits(1);
        slider = new JSlider(1, 100, 50);
        slider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                disque.setRayon(slider.getValue());
                valeurRayon.setText(Integer.toString(slider.getValue()));
                valeurPerimetre.setText(format.format(2 * Math.PI * slider.getValue()));
                valeurSurface.setText(format.format(Math.PI * slider.getValue() * slider.getValue()));
            }
        });
        
        disque = new DisquePanel(50);
        
        rayon = new JLabel("Rayon : ");
        valeurRayon = new JLabel("50");
        perimetre = new JLabel("Périmètre : ");
        valeurPerimetre = new JLabel(format.format(2 * Math.PI * slider.getValue()));
        surface = new JLabel("Surface : ");
        valeurSurface = new JLabel(format.format(Math.PI * slider.getValue() * slider.getValue()));
        JPanel info = new JPanel();
        info.setLayout(new GridLayout(3, 2));
        info.add(rayon);
        info.add(valeurRayon);
        info.add(perimetre);
        info.add(valeurPerimetre);
        info.add(surface);
        info.add(valeurSurface);
        
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(disque, BorderLayout.CENTER);
        getContentPane().add(slider, BorderLayout.SOUTH);
        getContentPane().add(info, BorderLayout.EAST);
        
        // Propriétés de la fenêtre.
        setLocation(50, 50);
        setSize(400, 280);
        setVisible(true);
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private class DisquePanel extends JComponent {
        private int rayon;
        public DisquePanel(int r) {
            rayon = r;
        }
        public void paint(Graphics g) {
            g.setColor(Color.BLUE);
            g.fillOval(5, 5, rayon * 2, rayon * 2);
        }
        
        public void setRayon(int r) {
            rayon = r;
            repaint();
        }
    }
    
    /**
     * <p>Débute l'exécution du programme.</p>
     * @param args Les paramètres de la ligne de commande.
     */
    public static void main(String[] args)
    {
        new Disque();
    }
}
